/*
 * @Author: fah 1742484532@qq.com
 * @Date: 2024-08-28 16:46:26
 * @LastEditors: fah 1742484532@qq.com
 * @LastEditTime: 2024-08-31 09:29:50
 * @FilePath: \livetree-admin-server\src\app.module.ts
 * @Description: 指飞部-企业数字化管理系统
 * @version: 1.0
 */
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { createClient } from 'redis';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LivesModule } from './lives/lives.module';
import { PhotosModule } from './photos/photos.module';
import { AuthModule } from './auth/auth.module';

import DatabaseConfig from './config/database';
@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: process.env.NODE_ENV === 'development' ? '.env.development' : '.env.production',
      isGlobal: true,
      load: [DatabaseConfig],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        return configService.get('database');
      },
      inject: [ConfigService],
    }),
    UsersModule,
    LivesModule,
    PhotosModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'REDIS_CLIENT',
      async useFactory() {
        const client = createClient({
          socket: {
            // host: 'localhost',
            // host: '192.168.1.6',
            // host: 'redis-container',
            host: process.env.REDIS_HOST,
            port: 6379,
          },
        });
        await client.connect();
        return client;
      },
    },
  ],
})
export class AppModule {}
